<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="模块名称" prop="eventName">
				<el-input v-model="form.eventName" placeholder="请输入模块名称" clearable/>
			</el-form-item>
			<el-form-item label="模块封面" prop="coverFile">
				<sc-upload v-model="form.coverFile" title="上传封面"/>
				<div class="el-form-item-msg" data-v-b33b3cf8="">支持JPG、PNG、BMP、TIF等50M以内</div>
			</el-form-item>
			<el-form-item label="模块详情图" prop="detailsFile">
				<sc-upload v-model="form.detailsFile" title="上传详情图"/>
				<div class="el-form-item-msg" data-v-b33b3cf8="">支持JPG、PNG、BMP、TIF等50M以内</div>
			</el-form-item>
			<el-form-item label="跳转链接" prop="handler">
				<el-input v-model="form.jumpLink" placeholder="请输入跳转链接" clearable/>
			</el-form-item>
			<el-form-item label="展示顺序" prop="sequence">
				<el-input v-model="form.sequence" placeholder="请输入展示顺序" clearable type="number"/>
			</el-form-item>
			<el-form-item label="是否启用" prop="status">
				<el-switch v-model="form.status" :active-value="1" :inactive-value="2"/>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import scCron from '@/components/scCron';

	export default {
		components: {
			scCron
		},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增模块',
					edit: '编辑模块'
				},
				form: {
					eventName: "",
					coverFile: "",
					detailsFile: "",
					state: "1"
				},
				rules: {
					eventName:[
						{required: true, message: '标题不能为空'}
					],
					coverFile:[
						{required: true, message: '封面不能为空'}
					],
					detailsFile:[
						{required: true, message: '详情不能为空'}
					]
				},
				visible: false,
				isSaveing: false,
				shortcuts: [
					{
						text: "每天8点和12点 (自定义追加)",
						value: "0 0 8,12 * * ?"
					}
				]
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate((valid) => {
					if (valid) {
						this.isSaveing = true;
						this.$API.operate.event.save.post(this.form).then(res => {
							this.isSaveing = false;
							if (res.success) {
								this.visible = false;
								this.$message.success("操作成功")
								this.$emit('success', this.form, this.mode)
							}
						})
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.eventName = data.eventName
				this.form.coverFile = data.eventCoverUrl
				this.form.detailsFile = data.eventDetailsUrl
				this.form.jumpLink = data.jumpLink
				this.form.sequence = data.sequence
				this.form.status = data.status
			}
		}
	}
</script>

<style>
</style>
